package pers.sen.cloud;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/11/26 20:04
 * @Description TODO
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
    // 注册RestTemplate
    @Bean
    // 用来拦截每个消费服务的restTemplate请求：启动Ribbon负载均衡（消费服务时，通过Ribbon负载均衡到对应的提供者）
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    // Ribbon使用随机策略
    @Bean
    public IRule rule(){
        return new RandomRule();
    }

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
        System.out.println("=========消费者启动============");
    }
}
